package com.stanyung.realtimeclient;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;

import org.apache.mina.util.byteaccess.ByteArray;

import com.stanyung.realtimeclient.common.SocketInvoker;
import com.stanyung.realtimeclient.io.DataReceiveHandler;
import com.stanyung.realtimeclient.io.RCSocketExceptionHandler;
import com.stanyung.realtimeclient.io.SocketExceptionHandler;
import com.stanyung.realtimeclient.io.XSocket;
import com.stanyung.realtimeclient.io.PacketReceiveHandler;

public class XClient extends JFrame implements ActionListener {

	public static final String CMD_CONNECT = "CMD_CONNECT";
	public static final String CMD_DISCONNECT = "CMD_DISCONNECT";
	public static final String CMD_LOGIN = "CMD_LOGIN";
	public static final String CMD_HEARTBEAT = "CMD_HEARTBEAT";
	private JButton btnConnect, btnDisconnect, btnLogin, btnHeartBeat;

	private SocketInvoker socketInvoker;
	
	public XClient() {
		setTitle("Chating");
		setLayout(new FlowLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		btnConnect = new JButton("Connect");
		btnConnect.setActionCommand(CMD_CONNECT);
		btnConnect.addActionListener(this);
		getContentPane().add(btnConnect);
		
		btnDisconnect = new JButton("Disconnect");
		btnDisconnect.setActionCommand(CMD_DISCONNECT);
		btnDisconnect.addActionListener(this);
		getContentPane().add(btnDisconnect);

		btnLogin = new JButton("Login");
		btnLogin.setActionCommand(CMD_LOGIN);
		btnLogin.addActionListener(this);
		getContentPane().add(btnLogin);
		
		btnHeartBeat = new JButton("HeartBeat");
		btnHeartBeat.setActionCommand(CMD_HEARTBEAT);
		btnHeartBeat.addActionListener(this);
		getContentPane().add(btnHeartBeat);

		setBounds(300, 200, 350, 200);
		setVisible(true);

		DataReceiveHandler dataReceiveHandler = new PacketReceiveHandler();
		SocketExceptionHandler exceptionHandler = new RCSocketExceptionHandler();
		socketInvoker = new XSocket(dataReceiveHandler, exceptionHandler);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (CMD_CONNECT.equals(cmd)) {
			socketInvoker.connect("127.0.0.1", 8088);
		} else if(CMD_DISCONNECT.equals(cmd)) {
			socketInvoker.disconnect();
		} else if (CMD_LOGIN.equals(cmd)) {
			StringBuffer sb = new StringBuffer();
			for(int a = 0; a < 20; a ++) {
				sb.append(" [").append(a).append("] ").append("Our objective is to maintain a high level and strict to accumulate male members,I easily get along with people, how about you? ");
			}
			String msg = sb.toString();
			System.out.println(Arrays.toString(msg.getBytes()));
			Packet packet = new Packet(1, 2, (byte)3, (byte)1, msg);
			socketInvoker.send(packet.toBytes());
			msg = "HelleWorld!";
			packet = new Packet(2, 2, (byte)3, (byte)1, msg);
			socketInvoker.send(packet.toBytes());
		} else if(CMD_HEARTBEAT.equals(cmd)) {
			Packet packet = Packet.newHeartBeatPacket();
			socketInvoker.send(packet.toBytes());
		}
	}

	public static void main(String[] args) {
		new XClient();
//		byte[] bs = {72, 101, 108, 108, 111, 87, 111, 114, 108, 100};
//		System.out.println(new String(bs));
	
//		0A 00 00 00 48 65 6C 6C 6F 57 6F 72 6C 64
		
	}

}

